package com.wjb.web.backend;


import com.wjb.model.EbNews;
import com.wjb.model.EbProduct;;

import com.wjb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2020-09-16 14:49
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 将数据放到 model中，将页面放到view中
     * spring mvc---将model渲染到view中
     * <p>
     * list ---分页请求数据
     */
    @RequestMapping("/List")
    public String list(Model model,
                       @RequestParam(value = "page", defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        System.out.println("start service.....;psize==>" + psize);
        //1.请求service层获取新闻的分页数据
        List<EbProduct> productList = productService.listByPage(psize, pageSize);

        int totalPage = productService.getMaxPage(pageSize);
        //2.传递model(数据)和view视图(jsp)
        model.addAttribute("productList", productList);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("curPage", psize);
        return "manage/product";//视图  通过manage/news.jsp进行展示数据
    }

    @RequestMapping("/to_save")
    public String toProductAdd() {
        return "manage/product-add";
    }

    @RequestMapping("/save")
    public String save(EbProduct ebProduct, MultipartFile pictureFile) {
        String pictureFile_name = pictureFile.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString() + pictureFile_name.substring(pictureFile_name.lastIndexOf("."));
        File uploadPic = new java.io.File("/Users/WJB/IdeaProjects/easybuy_www10  88/src/main/webapp/images/product/" + newFileName);
        try {
            if (!uploadPic.exists()) {
                uploadPic.mkdirs();
            }
            pictureFile.transferTo(uploadPic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用service层 保存商品信息
        ebProduct.setEpFileName(newFileName);
        Integer result = productService.save(ebProduct);
        if (result < 1) {
            return " ";
        } else {
            return "manage/manage-result";
        }
//        System.out.println("ebProduct==>" + ebProduct);
//        ebProduct.setEpcId(13);
//        ebProduct.setEpcChildId(15);
//        ebProduct.setEpName("关羽");
//        Integer result = productService.save(ebProduct);
//        if (result < 1) {
//            return " ";
//        } else {
//            return "manage/manage-result";git init
//        }
    }


    @RequestMapping("/to_edit")
    public String toEdit(Model model, @RequestParam("epId") Integer epId) {
        System.out.println("epId=" + epId);
        EbProduct ebProduct = productService.selectById(epId);
        model.addAttribute("ebProduct", ebProduct);
        return "manage/product-modify";
    }

    @RequestMapping("/edit")
    public String edit(EbProduct ebProduct) {
//       ebNews.setEnCreateTime(String.valueOf(new Date()));
        System.out.println(ebProduct);
        int result = productService.updateById(ebProduct);
        if (result <= 0) {

        }
        return "manage/manage-result";
    }

    @RequestMapping("/{id}")
    public String delete(@PathVariable("id") Integer id) {
        int result = productService.delete(id);
        if (result == 0) {

        }
        return "manage/manage-result";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Integer epId){
        //1.根据商品id查询商品的详情
        EbProduct ebProduct = productService.selectById(epId);
        //2.将商品信息传递给详情页面 product-new.jsp
        model.addAttribute("ebProduct",ebProduct);
        return "product-view";
    }
    @RequestMapping("/addCar")
    public String addCar(HttpServletRequest request, Integer epId){
        //1.根据id查询商品信息
        EbProduct productDetail = productService.selectById(epId);
        //2.将商品放入购物车(session)
        //2.1得到购物车
        HttpSession session = request.getSession();
        //2.2 得到存放商品的集合
        Set<EbProduct> productSet= (Set<EbProduct>) session.getAttribute("productSet");
        //2.2.1 如果购物车中没有set集合，那么重新创建一个新的set集合
        if(productSet==null){
            productSet= new HashSet<>(5);

        }
        //2.3 将商品放入集合
        for(EbProduct product:productSet){
            //2.3.1判断是否是重复的商品
            if(product.getEpId().equals(productDetail.getEpId())){
                //2.3.1.1 重复的商品将数量+1
                productDetail.setCount(product.getCount()+1);
                //2.3.1.2 移除set中原有的商品
                productSet.remove(product);
            }
        }
        productSet.add(productDetail);

        //2.5 将集合放入购物车
        session.setAttribute("productSet",productSet);
        return "shopping";
    }

}




